//
//  PlayVideoViewController.m
//  Cook
//
//  Created by 0.0 on 15-4-23.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "PlayVideoViewController.h"
#import "DownLoadModel.h"
#import "MyMoviePlayerController.h"

@interface PlayVideoViewController ()
@property (nonatomic, copy) NSString *video; // video文件夹路径
@property (nonatomic, copy) NSString *videoA; // 文件A的全名
@property (nonatomic, copy) NSString *videoB;
@property (nonatomic, retain) MyMoviePlayerController *moviePlayer; // 播放器
@end

@implementation PlayVideoViewController

- (void)dealloc
{
    [_downModel release];
    [_videoA release];
    [_videoB release];
    [_video release];
    [_moviePlayer release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self createPlayer];
}

- (void)createPlayer
{
    self.video = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    
    self.video = [NSString stringWithFormat:@"%@/%@", _video, @"video"];
    NSString *videoName = self.downModel.videoName;
    
    NSString *videoGoal = [self getNameFromVideoName:videoName];
    
    self.videoA = [NSString stringWithFormat:@"%@A.mp4", videoGoal];
    self.videoB = [NSString stringWithFormat:@"%@B.mp4", videoGoal];
    
    NSString *videoApath = [NSString stringWithFormat:@"%@/%@", _video, _videoA];
    NSURL *url = [[[NSURL alloc] initFileURLWithPath:videoApath] autorelease];
    self.moviePlayer = [[MyMoviePlayerController alloc] initWithContentURL:url];
    _moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    UIButton *view = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.height - 100, 0, 100, 50)];
    [view setTitle:@"<返回" forState:UIControlStateNormal];
    [view addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_moviePlayer.view addSubview:view];
    _moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    _moviePlayer.view.transform = CGAffineTransformConcat(_moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
    [_moviePlayer.view setFrame: self.view.bounds];

    
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer release];
    [_moviePlayer prepareToPlay];
    [_moviePlayer play];
    
}

// 返回按钮
- (void)buttonClick:(id)sender
{
    [self.goal.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:YES];
}

// 视频播放完成触发
- (void)moviePlayerFinish:(NSNotificationCenter *)noti
{
    NSString *videoBpath = [NSString stringWithFormat:@"%@/%@", _video, self.videoB];
    NSURL *url = [[[NSURL alloc] initFileURLWithPath:videoBpath] autorelease];
    _moviePlayer.contentURL = url;
    [_moviePlayer prepareToPlay];
    [_moviePlayer play];
}

// 取得视频名字(去掉A.mp4)
- (NSString *)getNameFromVideoName:(NSString *)videoName
{
    NSInteger count = [videoName length];
    NSString *name = [videoName substringToIndex:count - 5];
    return name;
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
